/**
 * 腾讯广告配置文件
 */

export const TENCENT_AD_CONFIG = {
  // 腾讯广告SDK地址 - 暂时禁用外部SDK
  SDK_URL: '', // 'https://qzs.gdtimg.com/union/res/union_cdn/page/js/gdt_union.js',
  
  // 应用配置 - 需要替换为实际的配置
  APP_ID: 'demo_app_id', // 腾讯广告主应用ID
  PLACEMENT_ID: 'demo_placement_id', // 广告位ID
  
  // 广告配置
  AD_CONFIG: {
    width: '100%',
    height: 'auto',
    autoRefresh: false,
    showCloseButton: false
  },
  
  // 点击有效期（小时）
  CLICK_VALIDITY_HOURS: 24,
  
  // 本地存储键名
  STORAGE_KEYS: {
    AD_CLICKED: 'mbti_ad_clicked',
    AD_CONFIG: 'mbti_ad_config'
  },
  
  // API端点
  API_ENDPOINTS: {
    AD_CLICK_REPORT: '/api/mbti/ad-click',
    AD_IMPRESSION: '/api/mbti/ad-impression'
  }
}

/**
 * 获取广告配置
 */
export const getAdConfig = () => {
  return {
    app_id: TENCENT_AD_CONFIG.APP_ID,
    placement_id: TENCENT_AD_CONFIG.PLACEMENT_ID,
    ...TENCENT_AD_CONFIG.AD_CONFIG
  }
}

/**
 * 检查广告点击状态
 */
export const checkAdClickStatus = () => {
  try {
    const adClickStatus = localStorage.getItem(TENCENT_AD_CONFIG.STORAGE_KEYS.AD_CLICKED)
    if (!adClickStatus) return false
    
    const clickData = JSON.parse(adClickStatus)
    const clickTime = new Date(clickData.timestamp)
    const now = new Date()
    const hoursDiff = (now - clickTime) / (1000 * 60 * 60)
    
    if (hoursDiff < TENCENT_AD_CONFIG.CLICK_VALIDITY_HOURS) {
      return true
    } else {
      // 超过有效期，清除记录
      localStorage.removeItem(TENCENT_AD_CONFIG.STORAGE_KEYS.AD_CLICKED)
      return false
    }
  } catch (error) {
    console.error('检查广告点击状态失败:', error)
    return false
  }
}

/**
 * 保存广告点击记录
 */
export const saveAdClickRecord = (recordId) => {
  const clickData = {
    timestamp: new Date().toISOString(),
    recordId: recordId,
    userAgent: navigator.userAgent,
    url: window.location.href
  }
  
  localStorage.setItem(TENCENT_AD_CONFIG.STORAGE_KEYS.AD_CLICKED, JSON.stringify(clickData))
  return clickData
}

/**
 * 上报广告点击事件
 */
export const reportAdClick = async (clickData) => {
  try {
    const response = await fetch(TENCENT_AD_CONFIG.API_ENDPOINTS.AD_CLICK_REPORT, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify({
        ...clickData,
        event_type: 'ad_click',
        platform: 'web'
      })
    })
    
    if (!response.ok) {
      throw new Error(`HTTP error! status: ${response.status}`)
    }
    
    return await response.json()
  } catch (error) {
    console.error('广告点击上报失败:', error)
    throw error
  }
}

/**
 * 上报广告展示事件
 */
export const reportAdImpression = async (impressionData) => {
  try {
    const response = await fetch(TENCENT_AD_CONFIG.API_ENDPOINTS.AD_IMPRESSION, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify({
        ...impressionData,
        event_type: 'ad_impression',
        platform: 'web'
      })
    })
    
    if (!response.ok) {
      throw new Error(`HTTP error! status: ${response.status}`)
    }
    
    return await response.json()
  } catch (error) {
    console.error('广告展示上报失败:', error)
    throw error
  }
}